<?php

namespace Home\Controller;

use Think\Controller;
use Think\Auth;
use Common\Service\Sms\ShortMessageService;

class IndexController extends Controller
{
    /**
     *函数名: index
     *函数描述:
     * @param int $_GET ['praise'] 活动的ID
     */
    public function index()
    {
        /*  $sms = new ShortMessageService();
         $result =  $sms->sendMsgTo('13750851157', '返回结果测试');
         */
        $this->display();
    }

    public function login()
    {
        if (IS_POST) {

            $user = M('users');

            $where['email'] = trim($_POST['email']);
            $password = md5(trim($_POST['password']));

            $currentUser = $user->where($where)->find();

            if ($currentUser && $currentUser['password'] === $password) {
                session(C('USER_AUTH_KEY'), $currentUser['id']);
                session('uname', $currentUser['username']);

                $now = time();
                $user->where(array('id' => $currentUser['id']))->save(array('lasttime' => $now, 'lastip' => htmlspecialchars(trim(get_client_ip()))));

                $this->success('登录成功', '/User/Index/index');
            } else {
                $this->error('账号不存在或密码错误！');
            }
        }
    }

    public function adminLogin()
    {

        if (IS_POST) {
            $email = $_POST['email'];
            $password = $_POST['password'];

            $admin = M('admin');
            $adminUser = $admin->where(array('email' => $email, 'password' => md5(trim($password))))->find();
            if ($adminUser) {
                session('admin', $adminUser['id']);

                $now = time();
                $admin->lasttime = time();
                $admin->lastip = htmlspecialchars(trim(get_client_ip()));
                $admin->save();
                $this->success('登录成功', '/Admin/Index/index');
            } else {
                $this->error('用户名不存在或密码错误');
            }
        }
        layout(false);
        $this->display();

    }

    //--小程序直播登录--
    public function xcx_livelogin()
    {
        $user = M('users');
        $where['email'] = trim(I("post.email", "", ""));
        $password = md5(I("post.password", "", ""));
        $currentUser = $user->where($where)->find();
        if ($currentUser && $currentUser['password'] === $password) {
            $this->ajaxReturn(["code" => 1]);
        } else {
            $this->ajaxReturn(["code" => -1]);
        }
    }
}
